package com.zrrt.game;

import java.io.IOException;
import java.util.Vector;
import common.*;
import wepbean.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class servlet extends HttpServlet implements Constant {
	/**
	 * Constructor of the object.
	 */
	HttpSession session;
	int r = 0;
	public servlet() {
		super();
	}

//	 Destruction of the servlet. <br>
	 
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String requestURL = (request.getRequestURL()).toString();
//		System.out.println(requestURL);
		String guestIP = RequestUtil.getIpAddr(request);
//		DBOperate dbo = new DBOperate();
		DBHandle db = new DBHandle();
//		获取当前时间
		  GetDateTime GetDateTime = new GetDateTime();
	      String nowtime = GetDateTime.getDate();
	      Game_access_logBean be=new Game_access_logBean();
	      String model = requestURL.substring(
					requestURL.lastIndexOf("/") + 1, requestURL
							.lastIndexOf("."));
		if (requestURL.endsWith(".html")) {
			
			String factory = null;
			
			boolean isMainPage = false;

			//查找当前机型对应的厂商并获得
			for (int i = 0; i < Constant.modelList.length; i++) {
				if (modelList[i][2].indexOf(model) >= 0) {
					isMainPage = true;
					factory = modelList[i][0];
					r = i;
					break;
				}
			}
			if (factory == null) {
				r = 0;
			}
		//	dbo.addLog(requestURL, guestIP);
			
	            be.setModel(model);
	            be.setUrl(requestURL); 
	            be.setDate(nowtime);
	            be.setIp(guestIP);
	            be.infor();
			String newUrl = "";
			newUrl = "/pages/wap1/mainpage.jsp?r=" + r + ""; 
			if(r>0){
				newUrl = "/pages/wap1/mainpage.jsp?r=" + r +"&model="+model+"";
			}
			//如果
			if(r==0){
				
			}
			request.getRequestDispatcher(newUrl).forward(request, response);
		} /*else if (requestURL.endsWith(".jads")) {
			String name = request.getParameter("name").toString();
			System.out.println(name);
			String url = request.getParameter("url").toString();
			dbo.adddownLoad(name, requestURL, guestIP);
			System.out.println(url);
			response.sendRedirect(url);
			
		} */else if (requestURL.endsWith(".jp")) {
             String r = request.getParameter("r").toString();
            // dbo.addLog(requestURL, guestIP);
             
	            be.setModel(model);
	            be.setUrl(requestURL); 
	            be.setDate(nowtime);
	            be.setIp(guestIP);
	            be.infor();
             String newUrl = requestURL.substring(requestURL.lastIndexOf("/")+1, requestURL.length()-1)+"sp?r="+r+"";
             request.getRequestDispatcher(newUrl).forward(request, response);
		} else if (requestURL.endsWith(".js")) {
			 String cid = request.getParameter("cid").toString();
          //   dbo.addLog(requestURL, guestIP);
			 be.setModel(model);
	            be.setUrl(requestURL); 
	            be.setDate(nowtime);
	            be.setIp(guestIP);
	            be.infor();
             String newUrl = requestURL.substring(requestURL.lastIndexOf("/")+1, requestURL.length())+"p?cid="+cid+"";
             request.getRequestDispatcher(newUrl).forward(request, response);
		}
		 else if (requestURL.endsWith(".xs")) {
			 String r = request.getParameter("r").toString();
           //  dbo.addLog(requestURL, guestIP);
			 be.setModel(model);
	            be.setUrl(requestURL); 
	            be.setDate(nowtime);
	            be.setIp(guestIP);
	            be.infor();
             String newUrl = requestURL.substring(requestURL.lastIndexOf("/")+1, requestURL.length()-2)+"jsp?r="+r+"";
             request.getRequestDispatcher(newUrl).forward(request, response);
		}
		 else if (requestURL.endsWith(".sp")) {
			 String phone = request.getParameter("phone").toString();
			 for (int i = 0; i < Constant.modelList.length; i++) {
					if (modelList[i][2].indexOf(phone) >= 0) {
						r = i;
						break;
					}
				}
          //   dbo.addLog(requestURL, guestIP);
			 be.setModel(model);
	            be.setUrl(requestURL); 
	            be.setDate(nowtime);
	            be.setIp(guestIP);
	            be.infor();
             String newUrl = requestURL.substring(requestURL.lastIndexOf("/")+1, requestURL.length()-2)+"jsp?r="+r+"";
             request.getRequestDispatcher(newUrl).forward(request, response);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}
}
